package com.payment.web.controller.app.merchant;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.payment.common.base.AjaxResult;
import com.payment.common.constant.Constants;
import com.payment.common.utils.aliyun.OSSClientUtil;
import com.payment.common.utils.file.FileUtils;
import com.payment.system.service.IMerchantImageService;
import com.payment.system.service.IMerchantService;

/**
 * @author fuzhili
 * @Description: app端商户： 登陆/注册/忘记密码/修改密码
 * @date 2019/1/17 8:54
 */
@Controller
@RequestMapping("/app/merchant")
public class LoginController {
	private static final Logger log = LoggerFactory.getLogger(LoginController.class);
	@Autowired
	private IMerchantService merchantService;
	@Autowired
	private IMerchantImageService merchantImageService;

	/**
	 * app用户登陆
	 *
	 * @param loginName
	 *            账号
	 * @param password
	 *            密码
	 * @param versionNo
	 *            版本号
	 * @param systemType
	 *            系统类型 【IOS/Android】 0 ios 1安卓
	 * @return
	 */
	@PostMapping("/login")
	@ResponseBody
	public AjaxResult login(String loginName, String password, String versionNo, String systemType,String isOnline) {
		return merchantService.login(loginName, password, versionNo, systemType,isOnline);
	}

	/**
	 * app商户注册
	 *
	 * @param phone      登录注册手机号
	 * @param password	   登录密码
	 * @param passwordNow 确认密码
	 * @param code		   手机验证码
	 * @param parentPhone 推荐人手机号
	 * @return
	 */
	@PostMapping("/register")
	@ResponseBody
	public AjaxResult register(String phone, String password, String passwordNow, String code, String parentPhone) {
		return merchantService.register(phone, password, passwordNow, code, parentPhone);
	}

	/**
	 * app端用户忘记密码
	 *
	 * @param phone  手机号
	 * @param password 密码
	 * @param passwordNow 确认密码
	 * @param code 验证码
	 * @return
	 */
	@PostMapping("/restPasswordByCode")
	@ResponseBody
	public AjaxResult restPasswordByCode(String phone, String password, String passwordNow, String code) {
		return merchantService.restPasswordByCode(phone, password, passwordNow, code);
	}

	/**
	 * 获取短信验证码
	 *
	 * @param phone 手机号
	 * @return
	 */
	@PostMapping(value = "/getMessageCode")
	@ResponseBody
	public AjaxResult getMessageCode(String phone) {
		return merchantService.getMessageCode(phone);
	}

	/**
	 * 根据原密码修改密码
	 *
	 * @param merchantId
	 *            商户id
	 * @param password
	 *            旧密码
	 * @param passwordNow
	 *            新密码
	 * @return
	 */
	@PostMapping("/updatePassword")
	@ResponseBody
	public AjaxResult updatePasswordByMerchantId(String merchantId, String password, String passwordNow) {
		return merchantService.updatePasswordByMerchantId(merchantId, password, passwordNow);
	}

	/**
	 * app端设置支付密码
	 *
	 * @param merchantId 用户ID
	 * @param password 密码
	 * @param passwordNow 确认密码
	 * @param code 验证码
	 * @return
	 */
	@PostMapping("/payPassword")
	@ResponseBody
	public AjaxResult payPassword(String merchantId, String password, String passwordNow, String code) {
		return merchantService.savePayPassword(merchantId, password, passwordNow, code);
	}

	/**
	 *  app修改支付密码
	 * @param merchantId   用户ID
	 * @param passwordOld   旧密码
	 * @param password    新密码
	 * @param passwordNow  确认密码
	 * @return
	 */
	@PostMapping("/updatePayPassword")
	@ResponseBody
	public AjaxResult updatePayPassword(String merchantId, String passwordOld, String password, String passwordNow) {
		return merchantService.updatePayPassword(merchantId, passwordOld, password, passwordNow);
	}

	/**
	 * 验证支付密码
	 *
	 * @param merchantId   用户ID
	 * @param payPassword  支付密码
	 * @return
	 */
	@PostMapping("/verificationPayPassword")
	@ResponseBody
	public AjaxResult verificationPayPassword(String merchantId, String payPassword) {
		return merchantService.verificationPayPassword(merchantId, payPassword);
	}

	/***
	 * 重置支付密码
	 *
	 * @param merchantId     用户ID
	 * @param phone    手机号
	 * @param password  密码
	 * @param passwordNow  支付密码
	 * @param code   验证码
	 * @return
	 */
	@PostMapping("/restPayPasswordByCode")
	@ResponseBody
	public AjaxResult restPayPasswordByCode(String merchantId, String phone, String password, String passwordNow,
											String code) {
		return merchantService.restPayPasswordByCode(merchantId, phone, password, passwordNow, code);
	}

	/**
	 * 上传头像
	 *
	 * @param merchantId  用户ID
	 * @param fileImg  头像图片
	 * @return
	 */
	@PostMapping("/updateHeadPortrait")
	@ResponseBody
	public AjaxResult updateHeadPortrait(String merchantId, MultipartFile fileImg) {
		try {
			if (fileImg == null || fileImg.isEmpty()) {
				return AjaxResult.error(1, "请上传图片");
			}
			if (!FileUtils.fileType(fileImg)) {
				return AjaxResult.error(1, "图片类型错误");
			}
			// 修改图片访问路径
			String imgUrl = OSSClientUtil.uploadImg2Oss(fileImg, Constants.MERCHANT_HEAD + merchantId+"/");
			return merchantImageService.updateHeadPortrait(merchantId, imgUrl);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("修改头像失败！请联系客服咨询", e);
			return AjaxResult.error("修改头像失败！请联系客服咨询");
		}
	}

	/**
	 * 环信注册
	 * @param loginName
	 * @return
	 */
	@PostMapping("/createNewIMUserSingle")
	@ResponseBody
	public AjaxResult createNewIMUserSingle(String loginName){
		return merchantService.createNewIMUserSingle(loginName);
	}




}
